package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.util.JwtUtil;
import com.example.util.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) {
            //用户名和密码合法
            //查询用户
            User u = userService.findByUserName(username);
            if(u == null){
                //没有占用
                //注册
                userService.register(username,password);
                return Result.success();
            }else {
                //被占用
                return Result.error("用户名被占用");
            }

    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password){
        //根据用户名查询用户
        User loginUser = userService.findByUserName(username);
        //判断用户是否存在
        if (loginUser == null) {
            return Result.error("用户名不存在");
        }

        //判断密码是否正确 loginUser对象中的password是英文
        if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){

            //登录成功
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(@RequestHeader(name = "Authorization") String token){
       //根据用户名查询用户
        Map<String, Object> map = JwtUtil.parseToken(token);
        String username = map.get("username").toString();

        User user = userService.findByUserName(username);
        return Result.success(user);
    }
}
